<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0; margin: 0;background-color: #2b292b;
        }
        canvas {
            background-color: #2b292b;
            display: block;
            margin: 0 auto;
            padding: 20px auto;
            border: 1px solid pink;
        }
    </style>
</head>
<body>
    <canvas id="briksCanvas" width="480" height="420"></canvas>
</body>
<!-- 所有的绘制在js代码中完成 -->
<script>
    // 获取2D画布的渲染上下文，参数只能是2d
    var canvas = document.getElementById("briksCanvas");
    var ctx = canvas.getContext("2d");

    var ballRadius = 10;
    var ballColor = "#f44280"

    var speed = 10;

    var x = canvas.width/2;
    var y = canvas.height-30;
    var dx = -2;
    var dy = -2;

    var paddleHeight = 15;
    var paddleWidth = 75;
    var paddleX = (canvas.width-paddleHeight)/2; //???

    var rightPressed = false;
    var leftPressed = false;

    // 砖块区域
    // 五行六列
    var brickRowCount = 5;
    var brickColumnCount = 6;
    // 一个砖块的宽和高
    var brickWidth = 70;
    var brickHeight = 20;
    var brickPadding = 6;

    var brickOffsetTop = 35;
    var brickOffsetLeft = 15;
    // 初始分数和生命
    var score = 0;
    var lives = 3;

    document.addEventListener("keydown",keyDownHandler,false);
    document.addEventListener("keyup",keyUpHandler,false);
    document.addEventListener("mousemove",mouseMoveHandler,false);

    var bricks = [];
    for (c = 0; c < brickColumnCount; c++) {
        bricks[c] = [];
        for (r = 0; r < brickRowCount; r++) {
            bricks[c][r] = {x:0, y:0,status:1}; //???
        }
    }

    // 监听键盘左右键行为
    function keyDownHandler(e) {
        if(e.keyCode == 39) {
            rightPressed = true;
        }
        else if (e.keyCode == 37) {
            leftPressed = true;
        }
    }

    function keyUpHandler(e) {
        if(e.keyCode == 39) {
            rightPressed = false;
        }
        else if (e.keyCode == 37) {
            leftPressed = false;
        }
    }

    // 绘制圆形小球
    function drawBall() {
        ctx.beginPath();
        // arc() 方法创建弧/曲线（用于创建圆或部分圆）
        ctx.arc(x,y,ballRadius,0,Math.PI*2);
        ctx.fillStyle = "#ddffa3";
        ctx.fill();
        ctx.closePath();
    }

    function drawPaddle() {
        ctx.beginPath();
        // rect()用于创建矩形
        ctx.rect(paddleX, canvas.height-paddleHeight,paddleWidth,paddleHeight); //???
        ctx.fillStyle = "#ddffa3";
        ctx.fill();
        ctx.closePath();
    }

    function drawBricks() {
        for (c = 0; c < brickColumnCount; c++) {
            for (r = 0; r < brickRowCount; r++) {
                if (bricks[c][r].status == 1) {
                    var brickX = (c * (brickWidth+brickPadding)) + brickOffsetLeft; //???
                    var brickY = (r * (brickHeight + brickPadding)) + brickOffsetTop;
                    bricks[c][r].x = brickX;
                    bricks[c][r].y = brickY;
                    ctx.beginPath();
                    ctx.rect(brickX,brickY,brickWidth,brickHeight);
                    ctx.fillStyle = "pink";
                    ctx.fill();
                    ctx.closePath();
                }
            }
        }
    }

    function drawScore() {
        ctx.font = "16px Arial";
        ctx.fillStyle = "#f44280";
        ctx.fillText("Score:" + score, 15, 20);
    }

    function drawLives() {
        ctx.font = "16px Arial";
        ctx.fillStyle = "#f44280";
        ctx.fillText("Lives:" +lives,canvas.width-70,20);
    }

    function drawGO() {
        ctx.font = "16px Arial";
        ctx.fillStyle = "#f44280";
        ctx.fillText("GAME OVER");
    }

    function mouseMoveHandler(e) {
        // MouseEvent.clientX 是只读属性， 它提供事件发生时
        // 的应用客户端区域的水平坐标 (与页面坐标不同)。
        var relativeX = e.clientX - canvas.offsetLeft; //???
        if(relativeX > 0 && relativeX < canvas.width) {
            paddleX = relativeX - paddleWidth/2;
        }
    }
    // 小球碰撞方向
    function collisionDetection() {
        for(c = 0; c < brickColumnCount; c++) {
            for(r=0; r < brickRowCount; r++) {
                var b = bricks[c][r];
                if(b.status == 1) {
                    // 小球需要撞击到方块偏中间位置才作数
                    if(x > b.x && x < b.x + brickWidth && y > b.y && y < b.y +
                     brickHeight) {
                        dy = -dy;
                        b.status = 0;
                        score ++;
                        // 满分刷新页面
                        if (score == brickRowCount * brickColumnCount) {
                            document.location.reload();
                        }
                     }
                }
            }
        }
    }

    function draw() {
        // clearRect() 方法清空给定矩形内的指定像素。
        // 在此处用来清除小球的运行轨迹像素
        ctx.clearRect(0,0,canvas.width,canvas.height);
        drawBall();
        drawPaddle();
        drawBricks();
        drawScore();
        drawLives();
        collisionDetection();

        if(x + dx >canvas.width-ballRadius || x + dx < ballRadius) {
            dx = -dx;
        }
        if (y + dy <ballRadius) {
            dy = -dy;
        }
        else if(y + dy > canvas.height - (ballRadius + paddleHeight)) {
            if(x > paddleX && x < paddleX + paddleWidth) {
                dy = -dy;
            }
            else {
                lives--;
                if(!lives) {
                    drawGO();
                }
                else{
                    x = canvas.width/2;
                    y = canvas.height - 30;
                    dx = 3;
                    dy = -3;
                    paddleX = (canvas.width - paddleWidth)/2;
                }
            }
        }

        if (rightPressed && paddleX < canvas.width-paddleWidth) {
            paddleX += 5;
        }
        else if(leftPressed && paddleX > 0) {
            paddleX -= 5;
        }
        x += dx;
        y += dy;
        requestAnimationFrame(draw);
    
    }

    draw();
</script>
</html>